//package net.haoxueba.xpricing.utils;
//
//import org.dom4j.Attribute;
//import org.dom4j.Document;
//import org.dom4j.DocumentException;
//import org.dom4j.Element;
//import org.dom4j.io.SAXReader;
//
//import java.io.*;
//import java.util.Iterator;
//import java.util.List;
//import java.util.function.Consumer;
//
//public class XmlUtils {
//
//    public static Document walkElement(InputStream inputStream, Consumer<Element> consumer) {
//        SAXReader reader = new SAXReader();
//        Document document = null;
//        try {
//            document = reader.read(inputStream);
//        } catch (DocumentException e) {
//            e.printStackTrace();
//            return null;
//        }
//        walkElement(document.getRootElement(), consumer);
//        return document;
//    }
//
//    public static Document walkElement(Reader reader1, Consumer<Element> consumer) {
//        SAXReader reader = new SAXReader();
//        Document document = null;
//        try {
//            document = reader.read(reader1);
//        } catch (DocumentException e) {
//            e.printStackTrace();
//            return null;
//        }
//        walkElement(document.getRootElement(), consumer);
//        return document;
//    }
//
//    public static String walkElement(String text, Consumer<Element> consumer) {
//        SAXReader reader = SAXReader.createDefault();
//
//        reader.setValidation(false);
//        Document document = null;
//        try {
//            document = reader.read(new StringReader(text));
//        } catch (DocumentException e) {
//            e.printStackTrace();
//            return null;
//        }
//        walkElement(document.getRootElement(), consumer);
//
//        StringWriter stringWriter = new StringWriter();
//        try {
//            document.write(stringWriter);
//
//        } catch (IOException e) {
//            e.printStackTrace();
//            return null;
//        }
//        return stringWriter.toString();
//    }
//
//    public static Element createElement(String text) {
//        SAXReader reader = SAXReader.createDefault();
//
//        reader.setValidation(false);
//        Document document = null;
//        try {
//            document = reader.read(new StringReader(text));
//            return document.getRootElement();
//        } catch (DocumentException e) {
//            e.printStackTrace();
//            return null;
//        }
//
//    }
//
//    public static Document createDocument(String text) {
//        SAXReader reader = SAXReader.createDefault();
//
//        reader.setValidation(false);
//        Document document = null;
//        try {
//            document = reader.read(new StringReader(text));
//            return document ;
//        } catch (DocumentException e) {
//            e.printStackTrace();
//            return null;
//        }
//
//    }
//
//    public static void walkElement(Document document, Consumer<Element> consumer) {
//        walkElement(document.getRootElement(), consumer);
//    }
//
//    public static void walkElement(Element element, Consumer<Element> consumer) {
//
//        if (consumer != null) {
//            consumer.accept(element);
//        }
//        Iterator<Element> itor = element.elementIterator();
//        while (itor.hasNext()) {
//            walkElement(itor.next(), consumer);
//        }
//    }
//
//    private static class Replacer implements Consumer<Element> {
//
//        @Override
//        public void accept(Element element) {
//            List<Attribute> attributes = element.attributes();
//            System.out.println("======获取属性值======");
//            for (Attribute attribute : attributes) {
//                System.out.println(attribute.getName() + "\t\t" + attribute.getValue());
//                if (attribute.getName().equalsIgnoreCase("tmpl")) {
//                    element.remove(attribute);
//                    element.addAttribute("th:text", attribute.getValue());
//                }
//            }
//        }
//    }
//
//    public static void main(String[] args) throws Exception {
//        //1.创建Reader对象
//        SAXReader reader = new SAXReader();
//        //2.加载xml
//        org.dom4j.Document document = reader.read(new File("/temp/2022/09/test.svg"));
//        //3.获取根节点
//        Element rootElement = document.getRootElement();
//        walkElement(rootElement, new Replacer());
////        Iterator iterator = ((org.dom4j.Element) rootElement).elementIterator();
////        while (iterator.hasNext()) {
////            Element stu = (Element) iterator.next();
////            List<Attribute> attributes = stu.attributes();
////
////            System.out.println("======获取属性值======");
////            for (Attribute attribute : attributes) {
////                System.out.println(attribute.getName() + "\t\t" + attribute.getValue());
////                if(attribute.getName().equalsIgnoreCase("tmpl")){
////
////                    stu.remove(attribute);
////                    stu.addAttribute("th:text",attribute.getValue());
////                }
////            }
////            System.out.println("======遍历子节点======");
////            Iterator iterator1 = stu.elementIterator();
////            while (iterator1.hasNext()) {
////                Element stuChild = (Element) iterator1.next();
////                System.out.println("节点名：" + stuChild.getName() + "---节点值：" + stuChild.getStringValue());
////            }
////        }
//        StringWriter stringWriter = new StringWriter();
//        document.write(stringWriter);
//        System.out.println(stringWriter.toString());
//    }
//}
